#!/bin/bash

# 定义服务器信息
k8s_master="192.168.6.207"
k8s_node=("192.168.4.246")

# 在每个node节点上生成 SSH 密钥对
for k8s_node in "${k8s_node[@]}"; do
    ssh $k8s_node "if [ ! -f ~/.ssh/id_rsa ]; then ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa; fi"
done

# 从每个node节点获取公钥并添加到master节点的 authorized_keys 文件中
for k8s_node in "${k8s_node[@]}"; do
    PUB_KEY=$(ssh $k8s_node "cat ~/.ssh/id_rsa.pub")
    ssh $k8s_master "mkdir -p ~/.ssh && echo $PUB_KEY >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
done

echo "配置完成:node节点可以免密钥登录master节点"

